package com.rr.sc.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.rr.sc.bo.UserBo;
import com.rr.sc.model.User;

@Controller
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    UserBo userBo;

    @RequestMapping(value = "/user/list", method = RequestMethod.GET)
    public ModelAndView list() {
	logger.info("Users list!");

	Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	if (auth != null) {
	    System.out.println(auth.getName());
	    System.out.println(auth.getAuthorities());
	    System.out.println(auth.isAuthenticated());
	    System.out.println(auth.getCredentials());
	    System.out.println(auth.getPrincipal());
	} else {
	    System.out.println("NOT AUTHENTICATED");
	}

	List<User> users = userBo.getAll();
	Map<String, Object> model = new HashMap<String, Object>();
	model.put("users", users);

	return new ModelAndView("user/list", model);
    }

    @Autowired
    public void setUserBo(UserBo userBo) {
	this.userBo = userBo;
    }

}
